import asyncio

import aiohttp

from com.arcfox.util import async_request as requests
from com.arcfox.util import constant


class BaiduLocation:
    def __init__(self, address, province, city, district):
        self.address = address
        self.province = province
        self.city = city
        self.district = district


async def point2location(point):
    '''
    :param point: 31.328525857711296,121.27077904393305(维度, 经度)
    :return:
    '''

    url = f"https://api.map.baidu.com/reverse_geocoding/v3/?ak={constant.BAIDU_MAP_AK}&output=json&location={point}"
    response = await requests.get_with_session(url, timeout=3)
    result_json = response.json()
    if result_json["status"] == 0:
        result = result_json['result']
        address = result['formatted_address']
        if address:
            ac = result['addressComponent']
            return BaiduLocation(address, ac['province'], ac['city'], ac['district'])
    return None


if __name__ == "__main__":
    asyncio.run(point2location('32.328525857711296,121.270779043931'))
